package core.views;

import javax.swing.*;
import java.awt.Font;
import java.awt.event.*;
import core.models.Load;
import core.Runner;
import java.io.IOException;

public class TitlePanel extends JPanel
{
	private JLabel titleLabel;
	private JButton loadButton, startButton;
	private Runner runner;
	private static final long serialVersionUID = 1L;
	
	public TitlePanel(Runner runner)
	{
		this.runner = runner;
		setLayout(null);
		ButtonListener listener = new ButtonListener();
		
		titleLabel = new JLabel("Oregon Trail");
		titleLabel.setFont(new Font("Tahoma", Font.PLAIN, 28));
		titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
		titleLabel.setBounds(67, 62, 318, 82);
		add(titleLabel);
		
		startButton = new JButton("Start Game");
		startButton.setBounds(162, 188, 120, 23);
		startButton.addActionListener(listener);
		add(startButton);
		
		loadButton = new JButton("Load Game");
		loadButton.setBounds(162, 246, 120, 23);
		loadButton.addActionListener(listener);
		add(loadButton);
		
	}
	
	private class ButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{	
			if (e.getSource() == startButton)
			{
				StartPanelMain.changeCard("Setup");
				StartPanelMain.showGameButton();
			}
			else if(e.getSource()==loadButton)
			{
				try
				{
					Runner tempRun = Load.load();
					System.out.println(runner.getGroup() + "initial");
					runner.setGroup(tempRun.getGroup());
					System.out.println(runner.getGroup());
					StartPanelMain.changeCard("Trail");
				}
				catch(IOException ex)
				{
					ex.printStackTrace();
				}
			}
		}
	}
}
